import sys
from PyQt6 import uic
from PyQt6.QtWidgets import (QApplication, QLineEdit, QComboBox, QGroupBox,
                             QFrame, QLabel, QWidget, QVBoxLayout, QSizePolicy, QGridLayout)

class LeftWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.init_ui()
    def init_ui(self):
        #创建布局
        layout = QVBoxLayout(self)
        self.group = QGroupBox()
        group_layout = QVBoxLayout(self.group)
        grid_layout = QGridLayout()
        group_layout.addLayout(grid_layout)
        self.frame = QFrame()
        frame_layout = QVBoxLayout(self)
        frame_layout.addWidget(self.group)
        layout.addWidget(self.frame)
        layout.setContentsMargins(0,0,0,0)
        #加载ui文件到当前部件
        try:

            uic.loadUi("定频通信参数设置.ui",self)

            self.set_background()
            self.apply_styles()
            self.setSizePolicy(QSizePolicy.Policy.Expanding,QSizePolicy.Policy.Expanding)
        except FileNotFoundError:
            print("ui文件未找到，使用备用布局")
            self.label = QLabel("定频通信参数设置",self)
            self.label.setStyleSheet("font-size:24px;text-align:center;")
            layout.addWidget(self.label)
            return



    def set_background(self):
        self.setStyleSheet("background-color:white;")

    def apply_styles(self):

        # 标签：边框颜色
        label = self.findChild(QLabel,"label")
        if label:
           label.setStyleSheet("border:1px solid #e0e0e0")

        # 输入框：圆角和边框修改
        line_edits = [
            self.findChild(QLineEdit,"lineEdit"),
            self.findChild(QLineEdit ,"lineEdit_2"),
            self.findChild(QLineEdit ,"lineEdit_3"),
            self.findChild(QLineEdit ,"lineEdit_4"),
            self.findChild(QLineEdit ,"lineEdit_5"),
            self.findChild(QLineEdit ,"lineEdit_6")
            ]
        for line_edit in line_edits:
            if line_edit:
                line_edit.setStyleSheet("border-radius: 3px;border:1px solid #d3d3d3")

        # 下拉框：红色箭头、箭头图标和边框颜色修改，添加下拉框内容
        all_combos = self.findChildren(QComboBox)
        for combo_edit in all_combos:
            combo_edit: QComboBox
            arrow_style = """
                        QComboBox::drop-down {
                            background-color: #cc0000;
                        }
                        QComboBox::down-arrow {
                            width:8px;
                            height:8px;
                            image:url(arrow.png);       
                         }
                         QComboBox {
                         color : #000000;
                         border:1px solid #d3d3d3;
                         }
                        """
            base_style = arrow_style
            if combo_edit.objectName() in ["comboBox_2","comboBox_3","comboBox_6","comboBox_7"]:
                base_style += """
                                   QComboBox {
                                   color: #808080;
                                   }"""
            combo_edit.setStyleSheet(base_style)

            if combo_edit.objectName() == "comboBox_2":
                combo_edit.addItem("4.8kbit/s")

            elif combo_edit.objectName() == "comboBox":
                combo_edit.addItem("message")

            elif combo_edit.objectName() == "comboBox_3":
                combo_edit.addItem("TX1")

            elif combo_edit.objectName() == "comboBox_4":
                combo_edit.addItem("128")

            elif combo_edit.objectName() == "comboBox_5":
                combo_edit.addItem("[ 7 3 2 1 0 ]")

            elif combo_edit.objectName() == "comboBox_6":
                combo_edit.addItem("4.8kbit/s")

            elif combo_edit.objectName() == "comboBox_7":
                combo_edit.addItem("RX1")

            elif combo_edit.objectName() == "comboBox_8":
                combo_edit.addItem("128")

            elif combo_edit.objectName() == "comboBox_9":
                combo_edit.addItem("[ 7 3 0 ]")


        # 修改GroupBox边框颜色
        groupbox_style = """QGroupBox {
                                 border:0.5px solid #999999;
                                 border-radius:2px;
                                 }"""
        groupbox1 = self.findChild(QGroupBox,"groupBox")
        groupbox2 = self.findChild(QGroupBox,"groupBox_2")

        if groupbox1:
            groupbox1.setStyleSheet(groupbox_style)
        if groupbox2:
            groupbox2.setStyleSheet(groupbox_style)

        # 修改Frame边框颜色
        myframe = self.findChild(QFrame,"frame")
        if myframe:
            myframe.setStyleSheet("""QFrame {
                                     border:0.5px solid #999999;
                                     border-radius:2px;
                                     }""")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    left = LeftWidget()
    left.show()
    sys.exit(app.exec())




